

-- Namespace
	local _,mod = ...


-- Constants
	local font = {mod.path..'media\\bavaria.ttf',8,'OUTLINEMONOCHROME'}
	
	local c_border = {.07,.07,.07}
	local c_deborder = {.60,.07,.07}


-- Helper
	local function OnEnter(self)
		GameTooltip:SetOwner(self,self.ttanchor or 'ANCHOR_CURSOR',self.ttx,self.tty)
		GameTooltip:SetUnitAura('player',self.spell,nil,self.filter)
	end
	
	local function OnLeave(self)
		GameTooltip:Hide()
	end


-- Init player
	mod.units['player'] = {}


-- Player: HELPFUL
	local bin = {}; mod.units['player']['HELPFUL'] = bin
	
	for i = 1,20 do
		local base = mod:CreateFrame('Frame',nil,UIParent)
			base:SetFrameLevel(1)
			base:SetHeight(16) base:SetWidth(16)
			base:SetPoint('TOPLEFT',8,-8-((i-1)*24))
			bin[#bin+1] = base
		
		local icon = base:CreateTexture(nil,'BORDER')
			icon:SetPoint('TOPLEFT',1,-1)
			icon:SetPoint('BOTTOMRIGHT',-1,1)
			icon:SetTexCoord(.1,.9,.1,.9)
			base.icon = icon
		
		local border = base:CreateTexture(nil,'BACKGROUND')
			border:SetAllPoints(true)
			border:SetTexture(unpack(c_border))
			base.border = border
		
		local timetext = base:CreateFontString(nil,'ARTWORK')
			timetext:SetPoint('BOTTOM',-3,-2)
			timetext:SetFont(unpack(font))
			timetext:SetJustifyH('RIGHT')
			base.timetext = timetext
		
		local stacktext = base:CreateFontString(nil,'OVERLAY')
			stacktext:SetPoint('TOP',5,3)
			stacktext:SetFont(unpack(font))
			stacktext:SetJustifyH('LEFT')
			base.stacktext = stacktext
		
		local function timer_update(elapsed)
			local self = base
			local min,max = self.tmin,self.tmax
			min = min + elapsed; self.tmin = min
			local left = max - min
			if left > 60 or left <= 0 then
				self.timetext:SetText('')
			else
				self.timetext:SetText(('%d'):format(left))
			end
		end
		
		base:Listen('aura',function(self,spell,icon,stacks,dtype,startT,endT)
			self.spell = spell
			
			self.icon:SetTexture(icon)
			
			if not stacks or stacks < 2 then
				stacks = ''
			end
			self.stacktext:SetText(stacks)
			
			if endT and endT > 0 then
				self.timetext:Show()
				self.tmin = GetTime() - startT
				self.tmax = endT - startT
				mod:timer_register(.2,true,timer_update)(0)
			else
				self.timetext:Hide()
				self.tmin = nil
				self.tmax = nil
				mod:timer_unregister(timer_update)
			end
		end)
	end
	
	-- Right-click cancelling
	local phauraheader = CreateFrame('Frame','NFAURAPLAYERHEADER',UIParent,'SecureAuraHeaderTemplate')
		phauraheader:ClearAllPoints()
		phauraheader:SetPoint('TOPLEFT',UIParent,'TOPLEFT',8,-8)
		phauraheader:SetAttribute('unit','player')
		phauraheader:SetAttribute('filter','HELPFUL')
		phauraheader:SetAttribute('template','NFAURAPLAYERSECURE')
		phauraheader:SetAttribute('minHeight',440)
		phauraheader:SetAttribute('minWidth',16)
		phauraheader:SetAttribute('point','TOPLEFT')
		phauraheader:SetAttribute('xOffset',0)
		phauraheader:SetAttribute('yOffset',-24)
		phauraheader:SetAttribute('wrapAfter',20)
		phauraheader:SetAttribute('wrapXOffset',0)
		phauraheader:SetAttribute('wrapYOffset',-8)
		phauraheader:SetAttribute('maxWraps',1)
		phauraheader:Show()


-- Player: HARMFUL
	local bin = {}; mod.units['player']['HARMFUL'] = bin
	
	for i = 1,20 do
		local base = mod:CreateFrame('Frame',nil,UIParent)
			base:SetFrameLevel(1)
			base:SetHeight(16) base:SetWidth(16)
			base:SetPoint('TOPLEFT',32,-8-((i-1)*24))
			bin[#bin+1] = base
			
			base.ttanchor = 'ANCHOR_BOTTOMRIGHT'
			base.ttx,base.tty = 16,8
			base.filter = 'HARMFUL'
			base:EnableMouse(true)
			base:SetScript('OnEnter',OnEnter)
			base:SetScript('OnLeave',OnLeave)
		
		local icon = base:CreateTexture(nil,'BORDER')
			icon:SetPoint('TOPLEFT',1,-1)
			icon:SetPoint('BOTTOMRIGHT',-1,1)
			icon:SetTexCoord(.1,.9,.1,.9)
			base.icon = icon
		
		local border = base:CreateTexture(nil,'BACKGROUND')
			border:SetAllPoints(true)
			border:SetTexture(unpack(c_deborder))
			base.border = border
		
		local timetext = base:CreateFontString(nil,'ARTWORK')
			timetext:SetPoint('BOTTOM',-3,-2)
			timetext:SetFont(unpack(font))
			timetext:SetJustifyH('RIGHT')
			base.timetext = timetext
		
		local stacktext = base:CreateFontString(nil,'OVERLAY')
			stacktext:SetPoint('TOP',5,3)
			stacktext:SetFont(unpack(font))
			stacktext:SetJustifyH('LEFT')
			base.stacktext = stacktext
		
		local function timer_update(elapsed)
			local self = base
			local min,max = self.tmin,self.tmax
			min = min + elapsed; self.tmin = min
			local left = max - min
			if left > 60 or left <= 0 then
				self.timetext:SetText('')
			else
				self.timetext:SetText(('%d'):format(left))
			end
		end
		
		base:Listen('aura',function(self,spell,icon,stacks,dtype,startT,endT)
			self.spell = spell
			
			self.icon:SetTexture(icon)
			
			if not stacks or stacks < 2 then
				stacks = ''
			end
			self.stacktext:SetText(stacks)
			
			if endT and endT > 0 then
				self.timetext:Show()
				self.tmin = GetTime() - startT
				self.tmax = endT - startT
				mod:timer_register(.2,true,timer_update)(0)
			else
				self.timetext:Hide()
				self.tmin = nil
				self.tmax = nil
				mod:timer_unregister(timer_update)
			end
		end)
	end


-- Player: HELPFUL (mine, time-sorted)
	local bin = {}; mod.units['player']['HELPFUL|PLAYER'] = bin
		bin.order = 'time_asc'
	
	for i = 1,8 do
		local base = mod:CreateFrame('Frame',nil,UIParent)
			base:SetFrameLevel(1)
			base:SetHeight(12) base:SetWidth(12)
			base:SetPoint('TOPLEFT',UIParent,'CENTER',-202,-47-((i-1)*15))
			bin[#bin+1] = base
			
			base.ttanchor = 'ANCHOR_CURSOR'
			base.filter = 'HELPFUL|PLAYER'
			base:EnableMouse(true)
			base:SetScript('OnEnter',OnEnter)
			base:SetScript('OnLeave',OnLeave)
		
		local icon = base:CreateTexture(nil,'BORDER')
			icon:SetPoint('TOPLEFT',1,-1)
			icon:SetPoint('BOTTOMRIGHT',-1,1)
			icon:SetTexCoord(.1,.9,.1,.9)
			base.icon = icon
		
		local border = base:CreateTexture(nil,'BACKGROUND')
			border:SetAllPoints(true)
			border:SetTexture(unpack(c_border))
			base.border = border
		
		local timetext = base:CreateFontString(nil,'ARTWORK')
			timetext:SetPoint('LEFT',base,'RIGHT',4,0)
			timetext:SetFont(unpack(font))
			timetext:SetJustifyH('LEFT')
			base.timetext = timetext
		
		local stacktext = base:CreateFontString(nil,'OVERLAY')
			stacktext:SetPoint('TOP',5,3)
			stacktext:SetFont(unpack(font))
			stacktext:SetJustifyH('LEFT')
			base.stacktext = stacktext
		
		local function timer_update(elapsed)
			local self = base
			local min,max = self.tmin,self.tmax
			min = min + elapsed; self.tmin = min
			local left = max - min
			if left > 60 or left <= 0 then
				self.timetext:SetText('')
			else
				self.timetext:SetText(('%d'):format(left))
			end
		end
		
		base:Listen('aura',function(self,spell,icon,stacks,dtype,startT,endT)
			self.spell = spell
			
			self.icon:SetTexture(icon)
			
			if not stacks or stacks < 2 then
				stacks = ''
			end
			self.stacktext:SetText(stacks)
			
			if endT and endT > 0 then
				self.timetext:Show()
				self.tmin = GetTime() - startT
				self.tmax = endT - startT
				mod:timer_register(.2,true,timer_update)(0)
			else
				self.timetext:Hide()
				self.tmin = nil
				self.tmax = nil
				mod:timer_unregister(timer_update)
			end
		end)
	end
